﻿using System;
using System.Windows.Forms;

namespace BGN.PlugIn.Client
{
    public partial class UpdateSinfoPassword : Form
    {
        #region "字段"

        private Model.User user = null;

        /// <summary>
        /// 布谷鸟系统的密码
        /// </summary>
        private String newPassword;

        #endregion

        #region "方法"

        public UpdateSinfoPassword(Model.User user,String newPassword)
        {
            InitializeComponent();
            this.user = user;
            this.newPassword = newPassword;
        }

        /// <summary>
        /// 取消按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        
        /// <summary>
        /// 确定按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (user.PasswordPlainText == textBox2.Text)
                {
                    this.user.Password = BasicObjects.MyEncrypt.EncryptMD5(this.newPassword).ToString();
                    this.user.PasswordPlainText = this.newPassword;
                    if (SinfoUserHelper.UpdateUser(this.user))
                    {
                        //this.user.SaveToRDB();
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        MessageBox.Show("成功同步布谷鸟系统密码到SInfo系统!", "提醒");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("同步布谷鸟系统密码到SInfo系统失败，请联系管理员!", "错误提醒");
                    }
                }
                else MessageBox.Show("输入密码不对，请确认输入SInfo系统的登陆密码", "提醒");
            }
            catch (Exception ex)
            {
                DreamCube.Foundation.Basic.Utility.MyLog.MakeLog(ex);
                MessageBox.Show("同步密码发生错误，请联系管理员", "错误提醒");
            }
        }

        /// <summary>
        /// 窗体加载的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateSinfoPassword_Load(object sender, EventArgs e)
        {
            if (user != null) textBox1.Text = user.UserName;
        }

        #endregion

        private void UpdateSinfoPassword_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }
}
